package com.mcookie.app.minicompo;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfView extends SurfaceView implements SurfaceHolder.Callback {
	
	static DrawThread drawT;
	SurfaceHolder mHolder;
	public static int radius = 10;
	public static Blur Surfview_BlurStyle = null;

    /**
     * xml생성자. 
     * @param context
     * @param attrs
     */
    public SurfView(Context context, AttributeSet attrs){
    	super(context, attrs);
    	mHolder = getHolder();
		mHolder.addCallback(this);
		
    }
    
    @Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		drawT = new DrawThread(mHolder);
		drawT.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		drawT.bExit = true; //�����÷��� ����.
		for(;;){
			try {
				drawT.join();
				break;
			} catch (InterruptedException e) {
				Log.d("surfTest", "join에러. " + e);
			}
		}
	}
	
	
	class DrawThread extends Thread{
		
		int w, h; 
		SurfaceHolder mHolder;
		Paint paint = new Paint();
		Paint paint2 = new Paint();
		BlurMaskFilter buBlurMaskFilter;
		boolean bExit;
		
		
		public DrawThread(SurfaceHolder surfaceHolder) {
			mHolder = surfaceHolder;
			bExit = false;
			paint.setColor(Color.WHITE);
			
			paint2.setColor(Color.BLACK);
		}
		
		public void run(){
			Canvas canvas;
			
			while (bExit == false) {
				
				synchronized (mHolder) {
					canvas = mHolder.lockCanvas(); 
					if(canvas == null){
						break;
					}
					canvas.drawRect(0, 0, 150, 150, paint2);
					
					if(Surfview_BlurStyle != null){
						buBlurMaskFilter = new BlurMaskFilter(20, Surfview_BlurStyle);
						paint.setMaskFilter(buBlurMaskFilter);						
					} else {
						paint.setMaskFilter(null);
					}
					
					canvas.drawCircle(75, 75, radius, paint);
					
					mHolder.unlockCanvasAndPost(canvas);
				}
			}
			
		}
	}
}